package com.cq.hd.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.system.po.TbWithdrawalConfigPo;
import com.cq.hd.system.mapper.TbWithdrawalConfigMapper;
import com.cq.hd.system.service.TbWithdrawalConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.system.api.dto.WithdrawalConfigSaveDto;
import com.cq.hd.system.api.vo.WithdrawalConfigVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 提现配置表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-19
 */
@Service
public class TbWithdrawalConfigServiceImpl extends ServiceImpl<TbWithdrawalConfigMapper, TbWithdrawalConfigPo> implements TbWithdrawalConfigService {

    @Override
    public WithdrawalConfigVo details(Integer withdrawalUserType, Integer financialAccountType) {
        if (withdrawalUserType == null) {
            Throw.isBusinessException("类型参数错误");
        }

        List<TbWithdrawalConfigPo> tbWithdrawalConfigPos = baseMapper.selectList(new QueryWrapper<TbWithdrawalConfigPo>().eq("del_state", 0)
                .eq("financial_account_type", financialAccountType)
                .eq("withdrawal_user_type", withdrawalUserType).orderByDesc("id"));
        if (!CollectionUtils.isEmpty(tbWithdrawalConfigPos)) {
            WithdrawalConfigVo withdrawalConfigVo = new WithdrawalConfigVo();
            BeanUtils.copyProperties(tbWithdrawalConfigPos.get(0), withdrawalConfigVo);

            return withdrawalConfigVo;
        }

        return null;
    }

    @Override
    public Long saveWithdrawalConfig(WithdrawalConfigSaveDto withdrawalConfigSaveDto) {
        BigDecimal minAmt = withdrawalConfigSaveDto.getMinAmt();
        BigDecimal singleMaxAmt = withdrawalConfigSaveDto.getSingleMaxAmt();
        Integer dayCount = withdrawalConfigSaveDto.getDayCount();
        BigDecimal commission = withdrawalConfigSaveDto.getCommission();
        BigDecimal autoApprovalAmt = withdrawalConfigSaveDto.getAutoApprovalAmt();
        // 提现自动审核方式(1-人工审核，2-自动审核)
        Integer autoApprovalType = withdrawalConfigSaveDto.getAutoApprovalType();

        if (minAmt == null || minAmt.compareTo(BigDecimal.ZERO) <= 0) {
            Throw.isBusinessException("起提金额必须大于0元");
        }

        if (minAmt.compareTo(new BigDecimal("99999999")) > 0) {
            Throw.isBusinessException("起提金额必须小于99999999元");
        }

        if (singleMaxAmt == null || singleMaxAmt.compareTo(BigDecimal.ZERO) <= 0) {
            Throw.isBusinessException("单次最高可提金额必须大于0元");
        }

        if (singleMaxAmt.compareTo(new BigDecimal("99999999")) > 0) {
            Throw.isBusinessException("单次最高可提金额必须小于99999999元");
        }

        if (dayCount == null || dayCount <= 0) {
            Throw.isBusinessException("单日可提次数必须大于0次");
        }

        if (commission == null || commission.compareTo(BigDecimal.ZERO) < 0) {
            Throw.isBusinessException("提现手续费必须大于等于0");
        }

        if (commission.compareTo(new BigDecimal("100")) > 0) {
            Throw.isBusinessException("提现手续费必须小于100");
        }

        if (autoApprovalType != null && autoApprovalType == 2) {
            if (autoApprovalAmt == null || autoApprovalAmt.compareTo(BigDecimal.ZERO) <= 0) {
                Throw.isBusinessException("自动审核提现金额必须大于0元");
            }
        }

        TbWithdrawalConfigPo tbWithdrawalConfigPo = baseMapper.selectById(withdrawalConfigSaveDto.getId());
        if (tbWithdrawalConfigPo == null) {
            Throw.isBusinessException("未找到提现配置数据");
        }

        BeanUtils.copyProperties(withdrawalConfigSaveDto, tbWithdrawalConfigPo);
        tbWithdrawalConfigPo.setUpdateTime(LocalDateTime.now());
        int update = baseMapper.updateById(tbWithdrawalConfigPo);
        if (update == 0) {
            Throw.isBusinessException("保存提现配置失败");
        }

        return tbWithdrawalConfigPo.getId();
    }
}
